#pragma once
//#include "stdafx.h"
#include "CellAgent.h"
#include "Grid.h"
#include "AdvancedDataLayer.h"
#include "CellAgent.h"
#include "Vector.h"
namespace LPS
{
	using namespace org::spark::core;
	using namespace org::spark::data;
	using namespace org::spark::space;
	using namespace org::spark::math;

	void CellAgent::_init()
	{
	}

	CellAgent::CellAgent() : SpaceAgent(0.5, SpaceAgent.CIRCLE)
	{
		InitializeInstanceFields();
		_init();
		this->heading = fRand(0.0,360.0);
		//this->setColor(SpaceAgent::BLACK);
	}

	void CellAgent::jump(double number)
	{
		this->move( Vector::getVector(number, this->heading));
	}

	void CellAgent::wiggle(double size)
	{
		this->heading += fRand(-45.0, 45.0);
		this->jump(size);
	}

	void CellAgent::sniff(Grid *data, double jumpSize)
	{
		Vector v = Vector();
		double ahead = 0;
		double left = 0;
		double right = 0;

		ahead = 0.0;
		right = 0.0;
		left = 0.0;
		org::spark::math::Vector v = Vector::getVector(1.0, this->heading);

		Vector *p=&v;
		v.add(*this->getPosition());
		
		ahead = data->getValue(p);
	    Vector _v;
		_v = Vector(Vector::getVector(1.0, (this->heading - 90.0)));
		_v.add(*this->getPosition());
		v = _v;
		p=&_v;
		right = data->getValue(p);
		Vector _v1;
		_v1 = Vector(Vector::getVector(1.0, (this->heading + 90.0)));
		_v1.add(*this->getPosition());
		v = _v1;
		p=&_v1;
		left = data->getValue(p);
		if (((right >= ahead) && (right >= left)))
		{
			this->heading -= 90.0;
		}
		else
		{
			if ((left >= ahead))
			{
				this->heading += 90.0;
			}
		}
		this->jump(jumpSize);
	}

	double fRand(double fMin, double fMax)
	{
				double f = (double)rand() / 1+RAND_MAX;
				return fMin + f * (fMax - fMin);
	}

	void CellAgent::InitializeInstanceFields()
	{
		age = 0;
		heading = 0;
	}
}
